/*=============================================================================
#    The element-comm is a tentative program used to test our ideas.           
#                       Copyright (C)  bgao                                    
#
#     This program is free software; you can redistribute it and/or            
#      modify it under the terms of the GNU General Public License             
#     as published by the Free Software Foundation; either version 2           
#         of the License, or (at your option) any later version.               
#
#     This program is distributed in the hope that it will be useful,          
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            
#             GNU General Public License for more details.                    
#
#    You should have received a copy of the GNU General Public License        
#     along with this program; if not, write to the Free Software             
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             
#                       MA  02110-1301, USA.                                  
#==============================================================================
# $Id: mac_poll.c 348 2010-11-29 09:01:16Z bgao $ 
#
# $Author: bgao $ 
#
# $Revision: 348 $ 
#
# $Date: 2010-11-29 17:01:16 +0800 (星期一, 29 十一月 2010) $ 
#
# Description: 
#
=============================================================================*/
/*! @file mac_poll.c
 *  @brief This file defines the MAC poll primitive
 */
#include "mac.h"

/*! mac poll request primitve, used to ask the coord is there are data for the device itself.
 *  if error occurs, it will be report to the upper layer through mac_poll_cfm().
 *  @param req pointer to the structure of poll request.
 *  @return return the status of the poll request.
 */
int8 mac_poll_req(mac_poll_req_t *req)
{
    mac_frm_data_req_t req_frm;
    uint16 short_addr = 0;
    int8 ret;
    buf_t *buf;
    uint8 en_ack;
    uint8 en_indir;
    mac_poll_cfm_t cfm;

    if (req == NULL) {
        return MAC_ERR;
    }

    buf = buf_alloc(true);
    if (buf == NULL) {
        ret = MAC_INVALID_PARAMETER;
        goto poll_req_err_process;
    }

    short_addr = mac_get_coord_short_addr();
    if (short_addr == USING_EXTENDED_ADDR) {
        req_frm.dest.mode = ADDR_64_M;
        req_frm.dest.a.addr64 = mac_get_coord_extended_addr();
    } else {
        req_frm.dest.mode = ADDR_16_M;
        req_frm.dest.a.addr16 = short_addr;
    }

    short_addr = mac_get_short_addr();
    if (short_addr == USING_EXTENDED_ADDR) {
        req_frm.src.mode = ADDR_64_M;
        req_frm.src.a.addr64 = mac_get_extended_addr();
    } else {
        req_frm.src.mode = ADDR_16_M;
        req_frm.src.a.addr16 = short_addr;
    }

    req_frm.pan_id_comp = 1;
    req_frm.dest_pan_id = mac_get_pan_id();
    req_frm.dsn = mac_get_dsn();

    ret = mac_gen_data_req(buf, &req_frm);
    if (ret != MAC_OK) {
        goto poll_req_err_process;
    }

    en_ack = 1;
    en_indir = 0;
    ret = mac_handle_tx(buf, &(req_frm.dest), en_ack, en_indir, req->handle, req_frm.dsn);
    if (ret != MAC_OK) {
        goto poll_req_err_process;
    }

    return MAC_OK;

poll_req_err_process:
    /* TODO confirm */
    cfm.status = ret;
    mac_poll_cfm(&cfm);

    buf_free(buf);
    return ret;
}

